var Table = {
	players : [],

	cx : 0,
	cy : 0,
	cr : 0,

	cards : [],
	currentPlayer : -1,
	currentRound : 0,
	
	cardStack : { x: 0, y: 0, _dummy: null },
	
	noiseImg : null,

	init : function() {
		var canvas = $g('c').add(this).ctx.canvas;
		this.updateCoords();
		this.onResize();
		
		this.cardStack.x = this.cx - Card.width / 2;
		this.cardStack.y = this.cy - Card.height - 60;
		this.cardStack._dummy = jQuery.extend(true, {}, Card);
		this.cardStack._dummy.init(this.cardStack.x, this.cardStack.y);

		var tmpcx = this.cx + 5 - (Card.width + 10) * 5 / 2;
		var tmpcy = this.cy - Card.height / 2;
		for ( var i = 0; i < 5; i++) {
			this.cards[i] = jQuery.extend(true, {}, Card);
			this.cards[i].init(tmpcx + i * (Card.width + 10), tmpcy);
		}
		
		this.noiseImg = Preloader.getImg('noise.png');
	},

	addPlayer : function(id, name, founds, sex) {
		var newPlayer = jQuery.extend(true, {}, Player);
		newPlayer.init(id, name, founds, sex);
		this.players.push(newPlayer);
		this.updatePlayerCoords();

		console.log('newPlaya ', id, name);
	},

	updateCoords : function() {
		var canvas = $g('c').ctx.canvas;
		this.cx = canvas.width / 2;
		this.cy = canvas.height / 2;
		this.cr = (canvas.height - 100) / 2 - Player.height / 2;
		this.updatePlayerCoords();
	},

	onResize : function() {
		// real resize
		// $('#c').attr('width',window.innerWidth-100);
		// $('#c').attr('height',window.innerHeight-100);
		// Table.updateCoords();

		// scale resize
		var c = $('#c');
		c.css('height', ((window.innerHeight - 100)) + 'px');
		c.css('width', ((window.innerHeight - 100) * c[0].width / c[0].height)
				+ 'px');
	},

	updatePlayerCoords : function() {
		var angle = Math.PI * 2 / this.players.length;
		for (i = 0; i < this.players.length; i++) {
			var iy = Math.round(this.cy + (this.cr + 50)
					* Math.sin(angle * i - Math.PI / 2))
					- Player.height / 2;
			var ix = Math.round(this.cx + (this.cr + 75)
					* Math.cos(angle * i - Math.PI / 2))
					- Player.width / 2;
			this.players[i].updateCoords(ix, iy);
		}
	},

	startPlayerTurn : function() {
		if (this.currentPlayer!=-1)
			this.players[this.currentPlayer].endTurn();
		//console.log(this.currentPlayer == this.players.length - 1)
		if (this.currentPlayer == this.players.length - 1) {
			Quene.add('table',this,function(){this.endRound();}, 150);
			Quene.add('table',this,function(){this.startRound();}, 150);
		} else {
			this.currentPlayer++;
		}
		this.players[this.currentPlayer].startTurn();
		
	},
	
	endRound : function() {
		this.currentPlayer = 0;
		this.currentRound++;
		for (i = 0; i < this.players.length; i++) this.players[i].endRound(i,this.players.length);
		this.collectCards();
	},
	startRound : function() {
		for (i = 0; i < this.players.length; i++) this.players[i].startRound(i,this.players.length);
		this.dealCards();
	},

	showFlop : function() {
		this.cards[0].show();
		var self = this;
		setTimeout(function(){self.cards[1].show()}, 200);
		setTimeout(function(){self.cards[2].show()}, 400);
	},
	showTurn : function() {
		this.cards[3].show();
	},
	showRiver : function() {
		this.cards[4].show();
	},
	
	hideTableCards : function() {
		var self = this;
		Quene.add('table',this,function(){this.cards[0].hide();}, 150);
		Quene.add('table',this,function(){this.cards[1].hide();}, 150);
		Quene.add('table',this,function(){this.cards[2].hide();}, 150);
		Quene.add('table',this,function(){this.cards[3].hide();}, 150);
		Quene.add('table',this,function(){this.cards[4].hide();}, 150);
	},

	collectCards : function() {
		var offset = this.players.length*700;
		var betweenCards = 150;
		Quene.add('c0',this,function(){this.cards[0].hide();}, offset);
		Quene.add('c0',this,function(){this.cards[0].moveToStack();}, 350);
		Quene.add('c1',this,function(){this.cards[1].hide();}, offset+betweenCards);
		Quene.add('c1',this,function(){this.cards[1].moveToStack();}, 350);
		Quene.add('c2',this,function(){this.cards[2].hide();}, offset+betweenCards*2);
		Quene.add('c2',this,function(){this.cards[2].moveToStack();}, 350);
		Quene.add('c3',this,function(){this.cards[3].hide();}, offset+betweenCards*3);
		Quene.add('c3',this,function(){this.cards[3].moveToStack();}, 350);
		Quene.add('c4',this,function(){this.cards[4].hide();}, offset+betweenCards*4);
		Quene.add('c4',this,function(){this.cards[4].moveToStack();}, 350);
	},
	dealCards : function() {
		var offset = this.players.length*1100;
		var single = 150;
		Quene.add('table',this,function(){this.cards[0].moveFromStack();}, offset);
		Quene.add('table',this,function(){this.cards[1].moveFromStack();}, single);
		Quene.add('table',this,function(){this.cards[2].moveFromStack();}, single);
		Quene.add('table',this,function(){this.cards[3].moveFromStack();}, single);
		Quene.add('table',this,function(){this.cards[4].moveFromStack();}, single);
	},

	/**
	 * gury method
	 */
	update : function() {
	},

	/**
	 * gury method
	 */
	draw : function(ctx, canvas) {
		ctx.fillStyle = "#054405";
		ctx.beginPath();
		ctx.arc(this.cx, this.cy, this.cr + 2, 0, Math.PI * 2);
		ctx.closePath();
		ctx.fill();

		ctx.fillStyle = "#106610";
		ctx.beginPath();
		ctx.arc(this.cx, this.cy, this.cr, 0, Math.PI * 2);
		ctx.closePath();
		ctx.fill();
		
		ctx.save();
		ctx.globalCompositeOperation = "source-atop";
		ctx.globalAlpha = 0.15;
		ctx.drawImage(this.noiseImg, 0, 0, ctx.canvas.width, ctx.canvas.height,0, 0, ctx.canvas.width, ctx.canvas.height);
		ctx.restore();
		
		ctx.font = '25px Geo';
		ctx.fillStyle = "#fff";
		ctx.textAlign = 'center';
		ctx.fillText('666 zl', this.cx, this.cy + Card.height / 2 + 40, 100);
	}
};